package org.wyf.EveryPoint.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.wyf.EveryPoint.common.entity.DictCategory;
import org.wyf.EveryPoint.common.entity.QueryRequest;

import org.wyf.EveryPoint.common.entity.DictItem;
import org.wyf.EveryPoint.common.mapper.DictItemMapper;
import org.wyf.EveryPoint.common.service.IDictCategoryService;
import org.wyf.EveryPoint.common.service.IDictItemService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import lombok.RequiredArgsConstructor;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * 字典表 Service实现
 *
 * @author wyf
 * @date 2021-04-21 15:46:11
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class DictItemServiceImpl extends ServiceImpl<DictItemMapper, DictItem> implements IDictItemService {

    private final DictItemMapper dictItemMapper;
    private final IDictCategoryService dictCategoryService;

    @Override
    public IPage<DictItem> findDictItems(QueryRequest request, DictItem dictItem) {
        LambdaQueryWrapper<DictItem> queryWrapper = new LambdaQueryWrapper<>();
        // TODO 设置查询条件
        Page<DictItem> page = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(page, queryWrapper);
    }

    @Override
    public List<DictItem> findDictItems(DictItem dictItem) {
	    LambdaQueryWrapper<DictItem> queryWrapper = new LambdaQueryWrapper<>();
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createDictItem(DictItem dictItem) {
        this.save(dictItem);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDictItem(DictItem dictItem) {
        this.saveOrUpdate(dictItem);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteDictItem(DictItem dictItem) {
        LambdaQueryWrapper<DictItem> wrapper = new LambdaQueryWrapper<>();
	    // TODO 设置删除条件
	    this.remove(wrapper);
	}

    @Override
    public List<DictItem> findDictItems(String typeKey) {
        QueryWrapper<DictCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("typekey",typeKey);
        Long id = dictCategoryService.getOne(queryWrapper).getId();
        QueryWrapper<DictItem> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("CATEGORY_ID",id);
        return this.baseMapper.selectList(queryWrapper1);
    }

    @Override
    public String findDictItemName(String typeKey, String code) {
        return this.baseMapper.findDictItemName(typeKey,code);
    }
}
